package com.orange.controller;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.orange.dbhelp.page.Page;
import com.orange.handler.AuthOrgUserService;
import com.orange.modules.entity.manage.AuthOrgUser;
import com.orange.util.Hash;
import com.orange.util.ReturnCode;

@Controller
@RequestMapping("/roleLogin")
public class AuthRoleLoginController extends BaseController{

	private Logger logger = Logger.getLogger( AuthRoleLoginController.class );
	
	@Autowired
	private AuthOrgUserService authOrgUserService;
	
	/**
	 * 添加领导帐号
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/saveNewUser" ,produces = MediaType.TEXT_HTML_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> saveNewUser(HttpServletRequest request,HttpServletResponse response ,@RequestBody Map<String ,String> map){
		String account = map.get("account");
		if(StringUtils.isBlank(account)){
			return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"account不可为空");
		}
		String type = map.get("type");
		if(StringUtils.isBlank(type)){
			return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"type不可为空");
		}
		String password = "123456";
		String newpass = Hash.sha2InHex(password);
		AuthOrgUser a = new AuthOrgUser();
		a.setUserAccount(account);
		a.setPassword(newpass);
		a.setType(type);
		a.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		authOrgUserService.saveAuthOrgUserInfo(a);
		
		return toResponse(null, ReturnCode.SUCCESS, "保存成功");
		
	}
	
	/**
	 * 分页查询
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/queryOrgUserPage" ,produces = MediaType.TEXT_HTML_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> queryOrgUserPage(HttpServletRequest request,HttpServletResponse response ,@RequestBody Map<String ,String> map){
		String pageSize = map.get("pageSize");
		String pageNumber = map.get("pageNumber");
		if(StringUtils.isBlank(pageNumber) || StringUtils.isBlank(pageSize)){
			return toResponse(null, ReturnCode.FAIL, "参数存在空值!");
		}
		try {
			Page<AuthOrgUser> page = authOrgUserService.queryAuthOrgUserByPage(map);
			return toResponse(page, ReturnCode.SUCCESS, "分页查询成功!");
		} catch (SQLException e) {
			e.printStackTrace();
			return toResponse(null, ReturnCode.FAIL, "分页查询失败!");
		}
	}
	
	/**
	 * 修改信息
	 */
	@RequestMapping(value="/updateOrgUserInfo" ,produces = MediaType.TEXT_HTML_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> updateOrgUserInfo(HttpServletRequest request,HttpServletResponse response ,@RequestBody Map<String ,String> map){
		String account = map.get("account");
		String pid = map.get("pid");
		AuthOrgUser ao = authOrgUserService.queryAuthOrgUserById(pid);
		ao.setUserAccount(account);
		authOrgUserService.saveAuthOrgUserInfo(ao);
		return toResponse(ao, ReturnCode.SUCCESS, "修改成功!");
	}
	
	/**
	 * 删除
	 */
	@RequestMapping(value="/deleteOrgUserInfo" ,produces = MediaType.TEXT_HTML_VALUE+";charset=UTF-8", method=RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<String> deleteOrgUserInfo(HttpServletRequest request,@RequestParam String pid){
		
		//String pid = map.get("pid");
		if(null == pid){
			return toResponse(null, ReturnCode.SUCCESS, "pid为null!");
		}
		AuthOrgUser a = authOrgUserService.queryAuthOrgUserById(pid);
		if(null == a ){
			return toResponse(null, ReturnCode.SUCCESS, "角色不存在!");
		}
		authOrgUserService.deleteAuthOrgUserInfo(pid);
		
		return toResponse(null, ReturnCode.SUCCESS, "删除成功!");
	}
}
